package indi.madoka.weeb.module.base.plugin;

import indi.madoka.weeb.annotations.Keyword;
import indi.madoka.weeb.annotations.Plugin;
import indi.madoka.weeb.bean.send.Sender;
import indi.madoka.weeb.bean.update.message.GroupMessage;
import indi.madoka.weeb.bean.update.message.UpdateMessage;
import indi.madoka.weeb.enums.MatchType;
import indi.madoka.weeb.module.base.service.CustomOpenAiService;

@Plugin("OpenAI Plugin")
public class OpenAiPlugin {

    private final CustomOpenAiService openAiService;

    public OpenAiPlugin(CustomOpenAiService openAiService) {
        this.openAiService = openAiService;
    }

    @Keyword(value = "#chat", param = " [关键字]",matchType = MatchType.STARTS_WITH, description = "使用ChatGPT进行聊天")
    public void chatWithSingle(UpdateMessage updateMessage) {
        String prompt = updateMessage.getRawMessage().substring(6);
        new Sender.Builder(updateMessage)
                .addText("少女祈祷中...")
                .build()
                .sendAll();
        new Sender.Builder(updateMessage)
                .addText(openAiService.getSingleChatReply(prompt, updateMessage.getUserId()))
                .build()
                .sendAll();
    }

    @Keyword(value = "#gchat", param = " [关键字]",matchType = MatchType.STARTS_WITH, description = "使用ChatGPT进行群聊")
    public void chatWithGroup(UpdateMessage updateMessage) {
        if(!(updateMessage instanceof GroupMessage)) {
            new Sender.Builder(updateMessage)
                    .addText("仅支持群聊")
                    .build()
                    .sendAll();
            return;
        }
        GroupMessage groupMessage = (GroupMessage) updateMessage;
        String prompt = updateMessage.getRawMessage().substring(6);
        new Sender.Builder(updateMessage)
                .addText("少女祈祷中...")
                .build()
                .sendAll();
        new Sender.Builder(updateMessage)
                .addText(openAiService.getGroupChatReply(prompt, groupMessage.getGroupId()))
                .build()
                .sendAll();
    }

    @Keyword(value = "#share", matchType = MatchType.EQUALS, description = "分享当前的个人语境到群聊")
    public void doShare(UpdateMessage updateMessage) {
        if(!(updateMessage instanceof GroupMessage)) {
            new Sender.Builder(updateMessage)
                    .addText("仅支持群聊")
                    .build()
                    .sendAll();
            return;
        }
        GroupMessage groupMessage = (GroupMessage) updateMessage;
        new Sender.Builder(updateMessage)
                .addText(openAiService.shareContext(updateMessage.getUserId(), groupMessage.getGroupId()))
                .build()
                .sendAll();
    }

    @Keyword(value = "#绘图", param = " [关键字]",matchType = MatchType.STARTS_WITH, description = "指定关键字绘图")
    public void createImage(UpdateMessage updateMessage) {
        String url = openAiService.getCreateImage(updateMessage.getRawMessage().substring(4));
        if(url.matches("https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")) {
            new Sender.Builder(updateMessage)
                    .addImage(url)
                    .build()
                    .sendAll();
        }else {
            new Sender.Builder(updateMessage)
                    .addText(url)
                    .build()
                    .sendAll();
        }
    }

    @Keyword(value = "#clr", param = " [,g]",matchType = MatchType.STARTS_WITH, description = "清除语境(g:群聊)")
    public void clearContext(UpdateMessage updateMessage) {
        String tag = updateMessage.getRawMessage().substring(5).trim();
        if("g".equals(tag) && updateMessage instanceof GroupMessage) {
            openAiService.clear(CustomOpenAiService.getGroupKeyPrefix(), ((GroupMessage)updateMessage).getGroupId());
            new Sender.Builder(updateMessage)
                    .addText("已清除群聊语境，现在可以开始新的聊天了")
                    .build()
                    .sendAll();
            return;
        }else if("g".equals(tag)) {
            new Sender.Builder(updateMessage)
                    .addText("仅支持群聊")
                    .build()
                    .sendAll();
            return;
        }
        openAiService.clear(CustomOpenAiService.getSingleKeyPrefix(), updateMessage.getUserId());
        new Sender.Builder(updateMessage)
                .addText("已清除个人语境，现在可以开始新的聊天了")
                .build()
                .sendAll();
    }
}
